/*
 * ******************************************************
 *   Copyright ©, 2017-2020, 安徽财金信息科技有限公司
 * ******************************************************
 */
package com.wy.poties.utils;

import com.alibaba.fastjson.JSONReader;

import java.io.*;
import java.util.LinkedList;
import java.util.List;

/**
 * <p>Title:</p>
 * 一句话功能说明
 * <p>Description:</p>
 * 功能描述
 *
 * @author cj021_汪洋
 * @version 1.0
 * @date 2020-11-09 17:10
 */
public class Jsonpare {
    //读取json文件
    public static String readJsonFile(String fileName) {
        String jsonStr = "";
        try {
            File jsonFile = new File(fileName);
            FileReader fileReader = new FileReader(jsonFile);
            Reader reader = new InputStreamReader(new FileInputStream(jsonFile), "utf-8");
            int ch = 0;
            StringBuffer sb = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            fileReader.close();
            reader.close();
            jsonStr = sb.toString();
            return jsonStr;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static <T> List<T> getOrderObjectsByClass( String jsonPath, Class<T> type){
        LinkedList<T> returnList = new LinkedList<T>();
        File file = new File(jsonPath);
        InputStreamReader isr = null;
        BufferedReader bufferedReader = null;
        try {
            isr = new InputStreamReader(new FileInputStream(file), "utf-8");
            bufferedReader = new BufferedReader(isr);

            JSONReader reader = new JSONReader(bufferedReader);
            reader.startArray();
            while (reader.hasNext()) {
                T readObject = reader.readObject(type);
                returnList.add(readObject);
            }
            reader.endArray();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally{
            try {
                if(null != isr){
                    isr.close();
                }
                if(null != bufferedReader){
                    bufferedReader.close();
                }
            } catch (Exception e2) {
            }
        }
        return returnList;
    }

}